﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
/// <summary>
/// Created By : Nguyen Dinh Tuong Uyen
/// Created Date : 02/22/2012
/// Description : Includes methods interact with SP data 
/// </summary>
namespace Common
{
    public static class DataAccess
    {
        public static DataTable GetTableFromCaml(string spCamlQuery, string listName, uint numRowLimit, params string[] arrFields)
        {
            SPWeb myWeb = new bzParameter().currentWeb;
            SPList myList = myWeb.Lists[listName];
            SPQuery myQuery = new SPQuery();
            myQuery.Query = spCamlQuery;
            myQuery.RowLimit = numRowLimit;
            myQuery.ViewFields = BuildCamlField(arrFields);

            SPListItemCollection items = myList.GetItems(myQuery);
            DataTable dt = new DataTable();
            dt = items.GetDataTable();

            return dt;
        }

        public static DataTable GetTableFromCaml(string spCamlQuery, string listName, params string[] arrFields)
        {
            SPWeb myWeb = new bzParameter().currentWeb;
            SPList myList = myWeb.Lists[listName];
            SPQuery myQuery = new SPQuery();
            myQuery.Query = spCamlQuery;
            myQuery.ViewFields = BuildCamlField(arrFields);

            SPListItemCollection items = myList.GetItems(myQuery);
            DataTable dt = new DataTable();
            dt = items.GetDataTable();

            return dt;
        }
        
        public static bool InsertListItem(string listName, List<string> UpdatedField
            , List<object> UpdatedValue, ref string strResult)
        {
            SPWeb myWeb = new bzParameter().currentWeb;
            SPList myList = myWeb.Lists[listName];
            bool result = false;
            try
            {
                SPListItem newItem = myList.AddItem();
                for (int i = 0; i < UpdatedField.Count; i++)
                {
                    newItem[UpdatedField[i]] = UpdatedValue[i];
                }
                newItem.Update();
                result = true;
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
                result = false;
            }
            finally
            {
                myWeb.Dispose();
            }

            return result;
        }

        public static bool UpdateListItem(string listName, int itemID, List<string> UpdatedField
            , List<object> UpdatedValue, ref string strResult)
        {
            SPWeb myWeb = new bzParameter().currentWeb;
            SPList myList = myWeb.Lists[listName];
            bool result = false;
            try
            {
                SPListItem newItem = myList.GetItemById(itemID);
                for (int i = 0; i < UpdatedField.Count; i++)
                {
                    newItem[UpdatedField[i]] = UpdatedValue[i];
                }
                newItem.Update();
                result = true;
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
                result = false;
            }
            finally
            {
                myWeb.Dispose();
            }

            return result;
        }

        private static string BuildCamlField(params string[] arrFields)
        {
            string fieldTemplate = "<FieldRef Name='{0}' />";
            string fieldsString = " ";

            for (int i = 0; i < arrFields.Length; i++)
            {
                fieldsString += string.Format(fieldTemplate, arrFields[i]);
            }

            return fieldsString;
        }
    }
}
